package com.xbongbong.saas.service.impl;/*/*/

import com.alibaba.fastjson.JSONArray;
import com.xbongbong.paas.pojo.UserSimpPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderCcGetDTO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderCcGetVO;
import com.xbongbong.saas.help.workflow.WorkOrderHelp;
import com.xbongbong.saas.model.WorkOrderCcModel;
import com.xbongbong.saas.service.WorkOrderCcService;
import com.xbongbong.sys.domain.entity.UserEntity;
import com.xbongbong.sys.model.UserModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author 魏荣杰
 * @date 2019/4/15 16:55
 * @since v1.0
 * @version v1.0
 */
@Service("workOrderCcService")
public class WorkOrderCcServiceImpl implements WorkOrderCcService {
    private static final Logger LOG = LoggerFactory.getLogger(WorkOrderCcServiceImpl.class);

    @Resource
    private UserModel userModel;
    @Resource
    private WorkOrderCcModel workOrderCcModel;
    @Resource
    private WorkOrderHelp workOrderHelp;

    @Override
    public void save(Long workOrderId, String corpid, JSONArray ccArray) throws XbbException {
        workOrderHelp.saveCC(workOrderId, corpid, ccArray);

    }

    @Override
    public WorkOrderCcGetVO getCc(WorkOrderCcGetDTO workOrderCcGetDTO) throws XbbException {
        WorkOrderCcGetVO workOrderCcGetVO = new WorkOrderCcGetVO();
        try {
            Long workOrderId = workOrderCcGetDTO.getDataId();
            String corpid = workOrderCcGetDTO.getCorpid();
            List<String> userIds = workOrderCcModel.getUserIds(corpid, workOrderId);
            List<UserEntity> userEntities = userModel.getByUserIdIn(corpid, userIds, true);
            List<UserSimpPojo> userSimpPojos = new ArrayList<>();
            for (UserEntity entity : userEntities) {
                UserSimpPojo userSimpPojo = new UserSimpPojo();
                userSimpPojo.setName(entity.getName());
                userSimpPojo.setUserId(entity.getUserId());
                userSimpPojo.setUserAvatar(entity.getAvatar());
                userSimpPojos.add(userSimpPojo);
            }
            workOrderCcGetVO.setCc(userSimpPojos);
        } catch (Exception e) {
            LOG.error("WorkOrderCcServiceImpl.getCc 出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return workOrderCcGetVO;
    }
}
